import Vue from 'vue'
import VueRouter from 'vue-router'
import Cinema from './Cinema'
import Movie from './Movie'
import Mine from './Mine'
import Register from './Register'
import My from './My'

Vue.use(VueRouter)


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
    Mine,
    Cinema,
    Movie,
    Register,
    My,
    {
        path:'*',
        redirect:'/mine'
    }
]

const router = new VueRouter({
    routes

})

// 路由守卫 
// 拦截一些没有权限的访问，有权限才能去指定路由
router.beforeEach((to, from, next) => {
    /* console.log(to);
    console.log(from); */
    let path = to.path;
    
    let username = localStorage.getItem('username');
    // console.log(username);

    // 设置默认为false,如果登录了则改为true
    let isLogin = false;
    if(username){
        isLogin = true
    }
    if(path == '/mine'){
        next()
    }else if(path == '/register'){
        next()
    }else{
        if(isLogin){
            next()
        }else{
            next('/mine')
        }
    }
    
    
})

export default router